<?php


namespace frontend\controllers;


use yii\helpers\ArrayHelper;
use yii\web\Controller;

class BinaryTreeController extends Controller
{
    public function actionIndex()
    {
        $list = [
            1,
            2,
            3,
            4,
            5,
            6,

        ];
        $tree = (new Tree())->create($list);
    }
}

class Note
{
    public ?string $data;
    public ?Note $leftChild;
    public ?Note $rightChild;

    public function __construct($data = null)
    {
        $this->data = $data;
    }
}

class Tree
{
    public Note $root;

    public function create($inputList)
    {
        $note = null;
        if (!empty($inputList)) {
            $data = array_shift($inputList);

            if ($data != null) {
                $note = new Note($data);
                $note->leftChild = $this->create($inputList);
                $note->rightChild = $this->create($inputList);
            }
        }

        return $note;
    }
}